package service;

import dao.ClientDao;
import dao.domain.Book;
import dao.domain.Client;
import dao.domain.Order;
import dao.domain.Sell;
import dao.impl.ClientDaoImpl;

import java.sql.SQLException;

/**
 * @author gzb
 */
public class ClientService {
    private static final ClientDao CLIENT_DAO = new ClientDaoImpl();


    /**
     * 充值
     *
     * @param client 操作对象
     * @param amount 充值金额
     */
    public static boolean recharge(Client client, double amount) {
        boolean flag = true;
        if (amount >= 0) {
            double clientAmount = client.getAmount();
            clientAmount = clientAmount + amount;
            client.setAmount(clientAmount);
        } else {
            flag = false;
        }

        return flag;
    }

    /**
     * 还书
     */
    public static void returnBook(Order order) throws SQLException {
        System.out.println("开始还书");
        Book book = order.getBook();
        assert book.isBorrow() : "book is not borrowed";
        book.setBorrow(false);
        int quantity = book.getQuantity();
        book.setQuantity(quantity + 1);
        BookService.update(book);
        OrderService.delete(order);

    }

    /**
     * 借阅
     *
     * @param client 操作对象
     * @param book   被操作对象
     * @return false失败，true成功
     */
    public static boolean borrow(Client client, Book book) throws SQLException {
        assert !book.isBorrow() : "book has been borrowed";
        boolean flag;
        if (book.isBorrow() || book.isSell()) {
            flag = false;
        } else {
            int quantity = book.getQuantity() - 1;
            book.setQuantity(quantity);
            if (quantity == 0) {
                book.setBorrow(true);
            }
            BookService.update(book);
            Order order = new Order();
            order.setClient(client);
            order.setBook(book);
            OrderService.save(order);
            flag = true;
        }
        return flag;
    }


    /**
     * 购买
     *
     * @param client 操作对象
     * @param book   被操作对象
     * @return false成功，true失败
     */
    public static boolean purchase(Client client, Book book) throws SQLException {
        assert !book.isSell() : "book has been sold";
        boolean flag;
        if (book.isSell() || client.getAmount() - book.getPrice() < 0 || book.isBorrow()) {
            System.out.println("fall to buy");
            flag = false;
        } else {
            System.out.println("can buy");
            int quantity = book.getQuantity() - 1;
            book.setQuantity(quantity);
            if (quantity == 0) {
                book.setSell(true);
                book.setBorrow(true);
            }
            BookService.update(book);
            consume(client, book.getPrice());
            Sell sell = new Sell(client, book);
            SellService.store(sell);
            flag = true;
        }
        return flag;
    }

    /**
     * 获得会员名称
     *
     * @param client 操作对象
     * @return 会员名称
     */
    public static String getMember(Client client) {
        String member;
        switch (getLevel(client)) {
            case 2:
                member = "gold member";
                break;
            case 3:
                member = "platinum member";
                break;
            case 1:
            default:
                member = "ordinary member";
                break;
        }
        return member;
    }

    /**
     * 查看客户会员等级
     *
     * @param client 被查询客户
     */
    public static int getLevel(Client client) {
        //level2的最低要求
        int minLevel2 = 1000;
        //level3的最低要求
        int minLevel3 = 10000;
        int level = 1;
        if (client.getConsumption() >= minLevel2) {
            level = 2;
        }
        if (client.getConsumption() >= minLevel3) {
            level = 3;
        }
        return level;
    }

    /**
     * client消费
     *
     * @param client      操作对象
     * @param consumption 消费金额
     */
    private static void consume(Client client, double consumption) throws SQLException {
        double discount = 1;
        int level = ClientService.getLevel(client);
        switch (level) {
            case 2:
                discount = 0.9;
                break;
            case 3:
                discount = 0.8;
                break;
            case 1:
            default:
                break;
        }
        double clientConsumption = client.getConsumption();
        clientConsumption += consumption * discount;
        double clientAmount = client.getAmount();
        clientAmount = clientAmount - consumption * discount;
        client.setAmount(clientAmount);
        client.setConsumption(clientConsumption);
        CLIENT_DAO.update(client);
    }

    public static void update(Client client) throws SQLException {
        CLIENT_DAO.update(client);
    }

    public static void store(Client client) throws SQLException {
        CLIENT_DAO.save(client);
    }
}
